<?php 
namespace Home\Controller;
class StoreController extends HomeController {
    public function index(){
        //新品上架
        $list1 = M()->table('dota2_goods ')->order('g_id desc')->join('dota2_goodsimg on  gi_g_id = g_id')->where('face = 1 and up = 1')->limit('12')->select();
        $this->assign('list1',$list1);

        //热销商品
        $list2 = M()->table('dota2_goods ')->join('dota2_goodsimg on  gi_g_id = g_id')->where('face = 1 and up = 1')->limit('12')->select();
        $this->assign('list2',$list2);
        
        //特色珍藏
        $newlist = M()->table('dota2_goods ')->join('dota2_goodsimg')->where('face = 1 and up = 1 and g_id = 8 and gi_g_id = g_id')->select();
        $this->assign('newlist',$newlist);
        //联赛
        $league = M()->table('dota2_goods ')->join('dota2_goodsimg')->where('g_type like "%联赛%" and up=1 and face = 1 and gi_g_id = g_id')->limit('5')->select();
        $this->assign('league',$league);

        //套装
        $suit = M()->table('dota2_goods ')->join('dota2_goodsimg')->where('g_type like "%套装%" and up=1 and face = 1 and gi_g_id = g_id')->limit('5')->select();
        $this->assign('suit',$suit);

        $this->display();
    }

    public function list(){
         $list = I('get.list');
         $mark = 0;
        if($list == 1){
            //英雄套装
            $data = M()->table('dota2_goods')->join('dota2_goodsimg')->where('(g_type like "%套装%" or g_type like "%信使%") and up=1 and face = 1 and gi_g_id = g_id')->select();
            $mark = 1;
        }else if($list == 2){
            //物品
            $data = M()->table('dota2_goods')->join('dota2_goodsimg')->where('(g_type like "%礼包%" or g_type like "%工具%") and up=1 and face = 1 and gi_g_id = g_id')->select();
            $mark = 2;
        }else if($list == 3){
            //职业商店
            $data = M()->table('dota2_goods ')->join('dota2_goodsimg')->where('g_type like "%联赛%" and up=1 and face = 1 and gi_g_id = g_id')->select();
            $mark = 3;
        }else{
            $search = $_POST['search'];
            $model = M('goods');
            $data = M()->table('dota2_goods')->join('dota2_goodsimg')->where('g_name like "%'.$search.'%" and face = 1 and gi_g_id = g_id and up =1')->select();
        }
        $this->assign('list',$data);
        $this->assign('mark',$mark);
        $this->display();
    }

    public function cart(){
        $email = $_SESSION['email'];
        $data = M()->table('dota2_user')->join('dota2_carts')->join('dota2_goods')->join('dota2_goodsimg')->where(' email="'.$email.'" and u_id = cu_id and cg_id = g_id and g_id = gi_g_id and face = 1 and up = 1')->select();
        $this->assign('cartlist',$data);
        $this->display();
    }

    public function detail(){
        $gid = $_GET['gid'];
        $newlist = M()->table('dota2_goods')->join('dota2_goodsimg')->where('up=1 and gi_g_id = g_id and g_id = "'.$gid.'"')->select();
        $this->assign('newlist',$newlist);
        $this->assign('detail',$newlist[0]['g_detail']);
        $this->assign('g_rank',$newlist[0]['g_rank']);
        $this->assign('gid',$newlist[0]['g_id']);
        $list = M()->table('dota2_appraise')->join('dota2_user_info')->where('app_u_id = ui_id and app_g_id ="'.$gid.'"')->select();
        $make = 0;
        $sum = 0;
        foreach($list as $k => $v){
            $iconpath = '/Uploads/person_icon/';
            $iconpath .= substr($v['icon'],0,8).'/';
            $iconpath .= $v['icon'];
            $list[$k]['icon'] = $iconpath;
            $sum = $sum + $v['app_star'];
            $make++;
        }
        if($make == 0){
            $star = '暂未评级';
        }else{
            $star = $sum/$make;
        }
        $this->assign('star',$star);
        $this->assign('list',$list);
        $this->display();
    }

    public function addCollection(){
        $gid = $_POST['gid'];
        $email = $_SESSION['email'];
        $uid = M('user')->where('email="'.$email.'"')->select();
        $uid = $uid[0]['u_id'];
        $model = M('collection');
        $list = $model->where('coll_uid = "'.$uid.'" and coll_gid ="'.$gid.'"')->select();
        if($list == false){
            $data['coll_uid'] = $uid;
            $data['coll_gid'] = $gid;
            $data['coll_time'] = time();
            $model->add($data);
            echo json_encode(1);
        }else{
            echo json_encode(2);
        }
    }
}